package com.example.demo.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.config.login.logincontroller;
import com.example.demo.datasource.multi.dao.usermapper;
import com.example.demo.datasource.multi.entity.user;
import com.example.demo.exception.CustomException;
import com.example.demo.result.R;
import com.example.demo.result.ResultCode;
import com.sun.istack.internal.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Slf4j
@RestController
public class uploadcontroller {

    @Autowired
    com.example.demo.config.login.logincontroller logincontroller;

    @Autowired
    com.example.demo.datasource.multi.dao.usermapper usermapper;

    @ResponseBody
    @PostMapping(value = {"/upload"})
    public R<String> upload(@NotNull MultipartFile file, HttpServletRequest request) {
        if (Objects.isNull(file)) {
            throw new CustomException(ResultCode.PARAM_MISS.getMessage());
        }
        //String url = minioUtil.upload(file, bizPath);

        //获取上传文件名
        String filename = file.getOriginalFilename();
        log.info("原文件名："+filename);
        //获取后缀名
        String sname = filename.substring(filename.lastIndexOf("."));
        //时间格式化格式
        SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss-SSS");
        //获取当前时间并作为时间戳
        String timeStamp=simpleDateFormat.format(new Date());
        //拼接新的文件名
        String newfname = "avatar"+timeStamp+sname;
        log.info("新文件名："+newfname);

        String path=System.getProperty("user.dir");
        File dir=new File(path+"\\files");
        if (!dir.exists()){
            dir.mkdir();
        }
        //新的文件路径,上传成功后也是新的文件url
        String newfilename = path+"\\files\\"+newfname;

        File newFile = new File(newfilename);
        log.info("文件路径："+newfilename);
        try {
            file.transferTo(newFile);
        } catch (IOException e) {
            e.printStackTrace();
        }

        JSONObject userInfo=logincontroller.getUserInfo(request);
        Map<String, Object> usermap = JSONObject.toJavaObject(userInfo, Map.class);
        Map<String, Object> result6 = new HashMap<String,Object>();
        result6 = (Map<String, Object>) usermap.get("result");
        int userid= (Integer) result6.get("id");

        String sql="update user a set a.avatar  = '"+newfname+"' WHERE a.id = "+userid+"";

        System.out.println("执行sql："+sql);
        int theuser=usermapper.uploadavatar(sql);
        System.out.println("成功插入："+theuser+"条");

        return R.success(newfilename);
    }
}
